package com.wanmait.cookbook.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.wanmait.cookbook.dao.AttentionDAO;
import com.wanmait.cookbook.dao.OfficialNewsDAO;
import com.wanmait.cookbook.dao.UserinfoDAO;
import com.wanmait.cookbook.dao.WorksCollectDAO;
import com.wanmait.cookbook.vo.Userinfo;

/**
 * Servlet implementation class AttentionServlet
 */
@WebServlet("/AttentionServlet")
public class AttentionServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AttentionServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		String action = request.getParameter("action");
		if ("judgeAttention".equals(action)) {
			this.judgeAttention(request, response);
		}else if ("cancelAttention".equals(action)) {
		      this.cancelAttention(request, response);
	    }
	    else if ("addAttention".equals(action)) {
	      this.addAttention(request, response);
	    }
	}
	 /**
	   *  鍙栨秷鍏虫敞
	   * @param request
	   * @param response
	   * @throws ServletException
	   * @throws IOException
	   */
	  protected void cancelAttention(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	    Userinfo userinfo = (Userinfo)request.getSession().getAttribute("userinfo");
	    String authorId = request.getParameter("authorId");
	    AttentionDAO.getAttentionDAO().cancelAttention(userinfo.getId(), Integer.parseInt(authorId));
	  }
	  



	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}
	  /**
     *  添加关注
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    protected void addAttention(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      Userinfo userinfo = (Userinfo)request.getSession().getAttribute("userinfo");
      if (userinfo==null) {
        request.getSession().setAttribute("URL", request.getHeader("Referer"));
        response.getWriter().print("no");
      }
      else {
        String authorId = request.getParameter("authorId");
        AttentionDAO.getAttentionDAO().addAttention(userinfo.getId(), Integer.parseInt(authorId));
        String con = "关注了你";
		OfficialNewsDAO.getOfficialNewsDAO().addAttentionNews(con, userinfo.getId(), Integer.parseInt(authorId));
      }
    }
	 /**
	   *  鍒ゆ柇鏄惁鍏虫敞浣滆��
	   * @param request
	   * @param response
	   * @throws ServletException
	   * @throws IOException
	   */
	  protected void judgeAttention(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       Userinfo userinfo = (Userinfo)request.getSession().getAttribute("userinfo");
	    String authorId = request.getParameter("authorId");
	    Boolean flag = AttentionDAO.getAttentionDAO().judgeAttention(userinfo.getId(), Integer.parseInt(authorId));
	    response.getWriter().print(flag);
	  }
}
